import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/model/health/sys_health_property.dart';
import 'package:flutter_eve_plugin/pages/health/actions.dart';
import 'package:flutter_eve_plugin/route/main.dart';
import 'package:flutter_eve_plugin/store/main.dart';
import 'package:flutter_eve_plugin/store_vm/app_vm.dart';
import 'package:flutter_eve_plugin/store_vm/health/main.dart';
import 'package:flutter_eve_plugin/utils/screen_util.dart';
import 'package:flutter_redux/flutter_redux.dart';

class MainHeaderDescWidget extends StatefulWidget {
  @override
  _MainHeaderDescWidgetState createState() => _MainHeaderDescWidgetState();
}

class _MainHeaderDescWidgetState extends State<MainHeaderDescWidget> {
  @override
  Widget build(BuildContext context) {
    return StoreConnector<AppState, AppStateViewModel>(converter: (store) {
      return AppStateViewModel(
        state: store.state,
        healthStateViewModel:
        HealthStateViewModel(pushSysPage: (String sysName) {
          int index;
          switch (sysName) {
            case '呼吸系统':
              index = 0;
              break;
            case '消化系统':
              index = 1;
              break;
            case '运动系统':
              index = 2;
              break;
            case '神经系统':
              index = 3;
              break;
            case '内分泌系统':
              index = 4;
              break;
          }
          store.dispatch(healthSysCurrPage(store, index));
          Navigator.pushNamed(context, RouteConstant.SysStateMainPage);
        }),
      );
    }, builder: (context, vm) {
      var list = vm.state.headlthState.propertyHealthModel.propertyHealthData
          ?.where((item) {
        return item.healthGrade <= 0;
      })?.toList();
      var list2 = vm.state.headlthState.propertyHealthModel.propertyHealthData
          ?.where((item) {
        return item.healthGrade == 1;
      })?.toList();
      var list3 = vm.state.headlthState.propertyHealthModel.propertyHealthData
          ?.where((item) {
        return item.healthGrade == 2;
      })?.toList();
      var list4 = vm.state.headlthState.propertyHealthModel.propertyHealthData
          ?.where((item) {
        return item.healthGrade == 3;
      })?.toList();
      if (list.length == 5) {
        //健康
        return Container(
          padding: EdgeInsets.only(
              left: setRatio(16), right: setRatio(16), bottom: setRatio(25)),
          child: Text(
            '很好，继续保持哦~',
            style: TextStyle(color: Color(0xFF866BFF), fontSize: setTextSp(12)),
          ),
        );
      } else if (list2.length > 0 && (list2.length + list.length == 5)) {
        List<TextSpan> widgetList = [];
        for (var i = 0; i < list2.length; i++) {
          PropertyHealthDataInfo item = list2[i];
          if (i == list2.length - 1) {
            //最后一项
            widgetList.add(TextSpan(
                text: '${item.healthSystem}',
                style: TextStyle(
                  color: Color(0xFFF73E3E),
                ),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    vm.healthStateViewModel.pushSysPage(item.healthSystem);
                  }));
          } else {
            widgetList.add(TextSpan(
                text: '${item.healthSystem}、',
                style: TextStyle(
                  color: Color(0xFFF73E3E),
                ),
                recognizer: TapGestureRecognizer()
                  ..onTap = () {
                    vm.healthStateViewModel.pushSysPage(item.healthSystem);
                  }));
          }
        }
        widgetList.add(TextSpan(
          text: '处于亚健康状态，随时可能生病。',
          style: TextStyle(
            color: Color(0xFF333333),
          ),
        ));
        //易生病
        return Stack(
          overflow: Overflow.visible,
          children: <Widget>[
            Container(
              width: setRatio(343),
              padding: EdgeInsets.only(
                  left: setRatio(16),
                  right: setRatio(16),
                  bottom: setRatio(25)),
              child: RichText(
                softWrap: true,
                textScaleFactor: 1.2,
                maxLines: 2,
                text: TextSpan(
                    text: '注意：',
                    style: TextStyle(
                      color: Color(0xFF333333),
                      fontSize: setTextSp(12),
                    ),
                    children: widgetList),
              ),
            ),
            Positioned(
                right: setRatio(-16),
                bottom: setRatio(5),
                child: GestureDetector(
                  onTap: (){
                    vm.healthStateViewModel.pushSysPage(list2?.first?.healthSystem);
                  },
                  child: Container(
                    width: setRatio(76),
                    height: setRatio(16),
                    decoration: BoxDecoration(
                        border: Border.all(
                            color: Color(0xFF8B88FE), width: setRatio(1)),
                        borderRadius:
                        BorderRadius.all(Radius.circular(setRatio(8)))),
                    child: Center(
                      child: Text(
                        '查看系统状态',
                        style: TextStyle(
                            color: Color(0xFF8B88FE), fontSize: setTextSp(11)),
                      ),
                    ),
                  ),
                )
            )
          ],
        );
      } else if (list3.length > 0) {
        //小病
        return Stack(
          overflow: Overflow.visible,
          children: <Widget>[
            Container(
              padding: EdgeInsets.only(
                  left: setRatio(16), right: setRatio(16), bottom: setRatio(25)),
              child: RichText(
                softWrap: true,
                textScaleFactor: 1.2,
                text: TextSpan(
                  text: '注意：',
                  style: TextStyle(
                    color: Color(0xFF333333),
                    fontSize: setTextSp(12),
                  ),
                  children: [
                    TextSpan(
                      text: '疾病值',
                      style: TextStyle(
                        color: Color(0xFFF73E3E),
                      ),
                    ),
                    TextSpan(
                      text: '增加到70%以上病情会加重哦。',
                      style: TextStyle(
                        color: Color(0xFF7975FF),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Positioned(
                right: setRatio(-16),
                bottom: setRatio(5),
                child: GestureDetector(
                  onTap: (){
                    vm.healthStateViewModel.pushSysPage(list3?.first?.healthSystem);
                  },
                  child: Container(
                    width: setRatio(53),
                    height: setRatio(16),
                    decoration: BoxDecoration(
                        border: Border.all(
                            color: Color(0xFF8B88FE), width: setRatio(1)),
                        borderRadius:
                        BorderRadius.all(Radius.circular(setRatio(8)))),
                    child: Center(
                      child: Text(
                        '查看病情',
                        style: TextStyle(
                            color: Color(0xFF8B88FE), fontSize: setTextSp(11)),
                      ),
                    ),
                  ),
                )
            )
          ],
        );
      } else if (list4.length > 0) {
        //大病
        return Stack(
          overflow: Overflow.visible,
          children: <Widget>[
            Container(
              width: setRatio(343),
              padding: EdgeInsets.only(
                  left: setRatio(16),
                  right: setRatio(16),
                  bottom: setRatio(25)),
              child: RichText(
                softWrap: true,
                textScaleFactor: 1.2,
                text: TextSpan(
                  text: '注意：',
                  style: TextStyle(
                    color: Color(0xFF333333),
                    fontSize: setTextSp(12),
                  ),
                  children: [
                    TextSpan(
                      text: '严重疾病',
                      style: TextStyle(
                        color: Color(0xFFF73E3E),
                      ),
                    ),
                    TextSpan(
                      text: '会影响日常生活哦~',
                      style: TextStyle(
                        color: Color(0xFF7975FF),
                      ),
                    ),
                  ],
                ),
              ),
            ),
            Positioned(
                right: setRatio(-16),
                bottom: setRatio(5),
                child: GestureDetector(
                  onTap: (){
                    vm.healthStateViewModel.pushSysPage(list4?.first?.healthSystem);
                  },
                  child: Container(
                    width: setRatio(53),
                    height: setRatio(16),
                    decoration: BoxDecoration(
                        border: Border.all(
                            color: Color(0xFF8B88FE), width: setRatio(1)),
                        borderRadius:
                        BorderRadius.all(Radius.circular(setRatio(8)))),
                    child: Center(
                      child: Text(
                        '查看病情',
                        style: TextStyle(
                            color: Color(0xFF8B88FE), fontSize: setTextSp(11)),
                      ),
                    ),
                  ),
                )
            )
          ],
        );
      } else {
        return Container();
      }
    });
  }
}
